package com.molyfun.parents.activity.store;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.store.adapter.ProductBriefAdapter;
import com.molyfun.parents.fragment.BaseFragment;
import com.molyfun.parents.rest.RestClient;
import com.molyfun.parents.rest.bean.UserProductListResponse;
import com.molyfun.parents.utils.LogUtils;
import com.molyfun.parents.view.ThreeTabBar;
import com.molyfun.parents.view.swipeRefreshLayout.SwipeRefreshLayout;

import java.lang.reflect.Field;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class StoreFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, SwipeRefreshLayout.OnLoadListener{

    public static final String TAG = StoreFragment.class.getSimpleName();
    @InjectView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    @InjectView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @InjectView(R.id.empty_layout)
    LinearLayout mEmptyLayout;

    LinearLayoutManager mLayoutManager;
    ProductBriefAdapter productBriefAdapter;

    private int mCurrentPage = RestClient.DEFAULT_PAGE;
    private boolean mHasMoreData = true;
    private int mCurrentCategory = ThreeTabBar.ThreeTabChooseTab.BAR_ONE;

    public StoreFragment() {
    }

    public static StoreFragment newInstance(int category) {
        StoreFragment fragment = new StoreFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("category", category);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_product_brief, container, false);
        ButterKnife.inject(this, view);

        mCurrentCategory = getArguments().getInt("category");

        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);

        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setOnLoadListener(this);
        mSwipeRefreshLayout.setTopColor(R.color.refresh_color_01, R.color.refresh_color_02,
                R.color.refresh_color_02, R.color.refresh_color_01);
        mSwipeRefreshLayout.setBottomColor(R.color.color_transparent, R.color.color_transparent,
                R.color.color_transparent, R.color.color_transparent);
        mSwipeRefreshLayout.setMode(SwipeRefreshLayout.Mode.BOTH);
        mSwipeRefreshLayout.setLoadNoFull(true);

        try {
            Field f = null;
            f = SwipeRefreshLayout.class.getDeclaredField("mTouchSlop");
            f.setAccessible(true);
            f.set(mSwipeRefreshLayout, 150);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        productBriefAdapter = new ProductBriefAdapter(getActivity(), mCurrentCategory);
        mRecyclerView.setAdapter(productBriefAdapter);
        loadData(true);
        return view;
    }

    public void onParentTabClick() {
        loadData(true);
    }

    @Override
    public void onRefresh() {
        mSwipeRefreshLayout.setRefreshing(false);
        mSwipeRefreshLayout.setLoading(false);
        loadData(true);
    }

    @Override
    public void onLoad() {
        if(mHasMoreData){
            mCurrentPage ++;
            mSwipeRefreshLayout.setRefreshing(false);
            mSwipeRefreshLayout.setLoading(false);
            loadData(false);
        }else{
            mSwipeRefreshLayout.setRefreshing(false);
            mSwipeRefreshLayout.setLoading(false);
        }
    }

    private String getRequestUrlParam(int mCurrentCategory){
        if(mCurrentCategory == ThreeTabBar.ThreeTabChooseTab.BAR_ONE){
            return "nexpire";
        }else if(mCurrentCategory == ThreeTabBar.ThreeTabChooseTab.BAR_TWO){
            return "pass";
        }else{
            return "expire";
        }
    }

    private void loadData(final boolean isRefresh){
        if(isRefresh){
            mCurrentPage = RestClient.DEFAULT_PAGE;
        }
        showProgressDialog();
        Call<UserProductListResponse> call = MFPApplication.getInstance().getRestClient().getUserProductList(getRequestUrlParam(mCurrentCategory) ,mCurrentPage, RestClient.DEFAULT_PAGE_SIZE);
        call.enqueue(new Callback<UserProductListResponse>() {
            @Override
            public void onResponse(Call<UserProductListResponse> call, Response<UserProductListResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null) {
                    if(isRefresh){
                        if( response.body().getData().size() > 0){
                            productBriefAdapter.refreshData(response.body().getData());
                            mHasMoreData = true;
                            mEmptyLayout.setVisibility(View.GONE);
                        }else{
                            mHasMoreData = false;
                            mEmptyLayout.setVisibility(View.VISIBLE);
                        }
                    }else{
                        if(response.body().getData().size() > 0){
                            mHasMoreData = true;
                        }else{
                            mHasMoreData = false;
                        }
                        if(response.body().getData().size()>0) {
                            productBriefAdapter.addAll(response.body().getData());
                        }
                    }

                }
                LogUtils.d(TAG,response.body().getCode()+"----->");
            }

            @Override
            public void onFailure(Call<UserProductListResponse> call, Throwable t) {
                dismissProgressDialog();
            }
        });

    }

}
